package examples_referal;

import org.codehaus.commons.compiler.*;

/**
 * Sample application which demonstrates how to use the
 * {@link org.codehaus.janino.ExpressionEvaluator ExpressionEvaluator} class.
 */
public final class ShippingCost {

    private ShippingCost() {}

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: <total>");
            System.err.println("Computes the shipping costs from the double value \"total\".");
            System.err.println("If \"total\" is less than 100.0, then the result is 7.95, else the result is 0.");
            System.exit(1);
        }

        // Convert command line argument to call argument "total".
        Object[] arguments = new Object[] { new Double(args[0]) };

        // Create "ExpressionEvaluator" object.
        IExpressionEvaluator ee = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
        ee.setExpressionType(double.class);
        ee.setParameters(new String[] { "total" }, new Class[] { double.class });
        ee.cook("total >= 100.0 ? 0.0 : 7.95");

        // Evaluate expression with actual parameter values.
        Object res = ee.evaluate(arguments);

        // Print expression result.
        System.out.println("Result = " + (res == null ? "(null)" : res.toString()));
    }
}
